﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AzukiMapUI
{
	public partial class ColorDialog : UserControl
	{
        Color _selectedColor;
        byte _alpha;
        double _opacity;

        #region constractor
        public ColorDialog()
		{
			// Required to initialize variables
			InitializeComponent();
            SetDefaults();
		}

        public ColorDialog(Brush brush)
        {
			// Required to initialize variables
			InitializeComponent();

            if (IsSolidColorBrush(brush))
            {
                Color c = GetColorFromBrush((SolidColorBrush)brush);
                _alpha = c.A;
                _opacity = (double)c.A / 255;
                _selectedColor = c;
            }
            else
            {
                SetDefaults();
            }
            SetSelectedFill(brush);
        }

        void SetDefaults()
        {
            _alpha = 255;
            _opacity = 1;
            _selectedColor = Color.FromArgb(255, 255, 0, 0);

            SetColorName("");
        }
        #endregion

        bool IsSolidColorBrush(Brush brush)
        {
            return ( brush.GetType() == typeof(SolidColorBrush));
        }

        Color GetColorFromBrush(SolidColorBrush brush)
        {
            return brush.Color;
        }

        bool CheckOpacity()
        {
            return FormInputControl.CheckNumberBetween(TbOpacity.Text, IconErrorOpacity, 0, 100);
        }

        void SetSelectedFill(Brush fill)
        {
            if (fill.GetType() == typeof(SolidColorBrush))
            {
                SolidColorBrush solidFill = (SolidColorBrush)fill;
                Color selectedColor = solidFill.Color;
                SetSelectedFill(selectedColor);
            }
            else
            {
                fill.Opacity = _opacity;
                RtSelectedColor.Fill = fill;
            }
        }

        void SetSelectedFill(Color c)
        {
            _selectedColor =  Color.FromArgb(_alpha, c.R, c.G, c.B);
            RtSelectedColor.Fill = new SolidColorBrush(_selectedColor);
        }


        void SetRgbText(Brush fill)
        {
            if (fill.GetType() == typeof(SolidColorBrush))
            {
                SolidColorBrush selectedFillSolid = (SolidColorBrush)fill;
                Color c = selectedFillSolid.Color;
                TbkRGB.Text = c.ToString();
                SetPicker(c);
            }
            else
            {
                TbkRGB.Text = "-";
            }
        }

        void SetPicker(Color selectedColor)
        {
            CpPicker.SelectedColor = selectedColor;
        }

        void SetColorName(Rectangle selectedRect)
        {
            string name = "";
            if (selectedRect.Resources["Name"] != null)
            {
                name = (string)selectedRect.Resources["Name"];
            }
            SetColorName(name);
        }

        void SetColorName(string name)
        {
            TbkColorName.Text = name;
        }

        private void CpPicker_ColorSelected(Color c)
        {
            c.A = (byte)_alpha;
            _selectedColor = c;
            //RtSelectedColor.Fill = new SolidColorBrush(c);
            SetSelectedFill( new SolidColorBrush(c));
            SetRgbText(RtSelectedColor.Fill);
            SetColorName("");
        }

        private void CpPallet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox lb = (ListBox)sender;
            Rectangle selectedRect = (Rectangle)lb.SelectedItem;
            if (selectedRect == null)
            {
                return;
            }
            Brush selectedFill = selectedRect.Fill;

            SetSelectedFill(selectedFill);
            SetRgbText(selectedFill);
            SetColorName(selectedRect);
        }

        private void TbOpacity_LostFocus(object sender, RoutedEventArgs e)
        {
            if (CheckOpacity())
            {
                _opacity = double.Parse(TbOpacity.Text) / 100;
                _alpha = (byte)((int)(_opacity * 255));
            }
        }

        private void IconAddPallet_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_selectedColor != null)
            {
                CpSelectedPallet.Add(_selectedColor, TbkColorName.Text, "");
            }
        }
	}
}